import { handleActions } from 'redux-actions'
import util from '../util'

const init = {
  period: {},
  objective: {},
  search: {},
  history: {},
  periodLevel: {},
  teamMembers: [],
  comments: null,
  commentsNumber: null,
  commentsIndex: 1,
  ancester: null,
  children: null,
  objectiveVOList: [], // 上一个周期的目标/结果
  copyLeadList: [], // 上级目标结果
  search: null,
  detail: null,
  remind: null,
  all: null,
  birdView: null,
  projectTeamList: [],
  teamGroups: [],
  groupMembers: [],
}

export default handleActions({

  'fetch team groups': (state, { payload }) => ({
    ...state,
    teamGroups: payload,
  }),

  'reset objective detail': (state) => ({
    ...state,
    detail: null
  }),

  'fetch objective bird view': (state, { payload } ) => {
    const { objectiveId, data } = payload;
    let current;
    let parent;
    function find(cb) {
      for(let i = 0;i < data.length;i++) {
        const arr = data[i];
        for(let j = 0;j < arr.length; j++) {
          const item = arr[j];
          const ret = cb(item, i, j);
          if (ret) {
            return;
          }
        }
      }
    }

    // find current
    find((item) => {
      const ret = item.objectiveId === objectiveId;
      if (ret) {
        current = item;
        return true;
      }
    });

    // find parent
    find((item, i, j) => {
      const ret = current.parentObjectiveId === item.objectiveId;
      if (ret) {
        data[i][j].isParent = true;
        return true;
      }
    });
    
    // find root
    find((item, i, j) => {
      const ret = !item.hasParent;
      if (ret) {
        data[i][j].isRoot = true;
        return true;
      }
    });

    return {
      ...state,
      birdView: data,
    }
  },

  // 获取所有目标，返回渲染数据
  'fetch all objective': (state, { payload }) => {
    return {
      ...state, 
      all: payload,
    }
  }, 

  // 获取某个用户的审批数据
  'fetch objective audit': (state, { payload }) => {
    return {
      ...state, 
      detail: payload,
    }
  }, 

  // 获取某个用户的所有目标，返回渲染数据
  'fetch target all objective': (state, { payload }) => {
    return {
      ...state, 
      all: payload,
    }
  }, 

  'clear periods': (state) => ({
    ...state,
    period: {
      ...state.period,
      objectivePeriods: [],
    }
  }),

  'fetch objective detail': (state, { payload }) => {
    let getObj = payload.objectiveVO;
    getObj.thumbup = payload.thumbup;
    return {
      ...state,
      detail: payload.objectiveVO
    }
  },

  'reset okr comments': (state) => ({
    ...state,
    comments: null,
    commentsNumber: null,
    commentsIndex: 1,
  }),

  // 修改点赞
  /* "update thumpBub": (state, { payload }) => {
    return {
      ...state,
      // objective: payload
    }
  }, */

  // 获取目标
  'fetch objective': (state, { payload }) => ({
    ...state,
    objective: payload,
  }),

  // 清空目标
  'reset objective': (state) => ({
    ...state,
    objective: {},
  }),
  // 获取之前的周期目标/结果
  'fetch previous objective': (state, { payload }) => ({
    ...state,
    objectiveVOList: payload,
    thumbup:payload
  }),
  // 重置之前的周期目标/结果
  'reset previous objective': (state) => {
    return {
      ...state,
      objectiveVOList: [],
      thumbup:[]
    }
  },
  // 获取上级目标结果
  'fetch copylead': (state, {payload}) => ({
    ...state,
    copyLeadList: payload,
  }),
  // 重置上级目标/结果
  'reset copylead': (state) => {
    return {
      ...state,
      copyLeadList: [],
    }
  },

  // 重置上级目标
  'reset objective search': (state) => {
    return {
      ...state,
      search: null,
    }
  },

  // 搜索上级目标
  'search objective': (state, { payload }) => ({
    ...state,
    search: payload,
  }),

  'reset objective relative': (state) => ({
    ...state,
    ancester: null,
    children: null,
  }),

  'fetch objective ancester': (state, { payload }) => ({
    ...state,
    ancester: payload
  }),

  'fetch objective children': (state, { payload }) => ({
    ...state,
    children: payload
  }),

  'fetch okr team members': (state, { payload }) => ({
    ...state,
    teamMembers: payload
  }),

  'update period level two': (state, { payload }) => ({
    ...state,
    periodLevel: {
      ...state.periodLevel,
      two: state.periodLevel.two.map(v => {
        if (v.name == payload.name) {
          return {
            ...v,
            default: true
          }
        }
        return {
          ...v,
          default: false
        }
      })
    }
  }),

  'update period level one': (state, { payload }) => ({
    ...state,
    periodLevel: {
      ...state.periodLevel,
      one: state.periodLevel.one.map(v => {
        if (v.type == payload) {
          return {
            ...v,
            default: true
          }
        }
        return {
          ...v,
          default: false
        }
      })
    }
  }),

  'fetch period level one': (state, { payload }) => ({
    ...state,
    periodLevel: {
      ...state.periodLevel,
      one: payload
    }
  }),

  'fetch period level two': (state, { payload }) => ({
    ...state,
    periodLevel: {
      ...state.periodLevel,
      two: payload
    }
  }),

  /* 'fetch objective new': (state, {payload}) => ({
    ...state,
    period: payload
  }), */

  'fetch period': (state, { payload }) => ({
    ...state,
    period: payload
  }),

  'clear okr teams and users': (state, { payload }) => ({
    ...state,
    search: {}
  }),

  'search okr teams and users': (state, { payload }) => ({
    ...state,
    search: payload
  }),

  'fetch okr history': (state, { payload }) => {
    return {
      ...state,
      history: payload
    }
  },

  'reset okr team members': (state) => ({
    ...state,
    teamMembers: []
  }),

  'reset okr search': (state) => ({
    ...state,
    history: {},
    search: {}
  }),

  'reset period level': (state, { payload }) => ({
    ...state,
    periodLevel: {} 
  }),

  // 重置所有的okr
	'reset okr': (state, { payload }) => ({
		...state,
		...init
	}),

  'reset period': (state, { payload }) => ({
    ...state,
    period: {}
  }),

  're fetch okr comments': (state, { payload }) => ({
    ...state,
    comments: payload.okrCommentVOList,
    commentsNumber: payload.totalRecordNum,
    commentsIndex: 2,
  }),

  'fetch okr comments': (state, { payload }) => ({
    ...state,
    comments: [
      ...(state.comments || []),
      ...payload.okrCommentVOList
    ],
    commentsNumber: payload.totalRecordNum,
    commentsIndex: state.commentsIndex + 1,
  }),
  'fetch objective remind': (state, { payload }) => ({
    ...state,
    remind: payload,
  }),
  'fetch project team':  (state, { payload }) => ({
    ...state,
    projectTeamList: payload,
  }),
  'add project team':  (state, { payload }) => ({
    ...state,
    projectTeamList: [payload, ...state.projectTeamList],
  }),
  'delete project team': (state, { payload }) => ({
    ...state,
    projectTeamList: state.projectTeamList.filter(v => v.projectTeamId !== payload),
  }),
  'update project team': (state, { payload }) => ({
    ...state,
    projectTeamList: state.projectTeamList.map(v => {
      if(v.projectTeamId === payload.projectTeamId){
        return {
          ...v,
          ...payload,
        }
      }else
        return v;
    }),
  }),
  'fetch project team member': (state, { payload }) => ({
    ...state,
    projectTeamList: state.projectTeamList.map(v => {
      if(v.projectTeamId === payload.projectTeamId){
        return {
          ...v,
          members: payload.list,
        }
      }else
        return v;
    }),
  }),
  'fetch group members': (state, { payload }) => ({
    ...state,
    groupMembers: payload,
  }),
}, init);
